﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;

namespace FYPHP
{
    public partial class ReferencePrinting : System.Web.UI.Page
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["fyphp"].ConnectionString);

        protected void Page_Load(object sender, EventArgs e)
        {
            SqlCommand cmdSelect;
            SqlDataReader dtrSelect;
            conn.Open();

            cmdSelect = new SqlCommand("SELECT * FROM Products p, Transactions t, Transactions_Products tp, Users u, Shipping_Details sd WHERE p.p_id=tp.p_id AND t.t_id=tp.t_id AND u.u_id=t.u_id AND t.s_id=sd.s_id AND t.t_id=@tid", conn);
            cmdSelect.Parameters.AddWithValue("@tid", Request.QueryString["id"].ToString());
            dtrSelect = cmdSelect.ExecuteReader();

            Repeater1.DataSource = dtrSelect;
            Repeater1.DataBind();
            dtrSelect.Close();

            dtrSelect = cmdSelect.ExecuteReader();
            dtrSelect.Read();
            lblRef.Text = "REF-" + String.Format("{0:00000}", Request.QueryString["id"].ToString());
            lblDate.Text = Convert.ToDateTime(dtrSelect["t_date"]).ToString("dd/MM/yyyy");
            lblRecipient.Text = dtrSelect["username"].ToString();
            lblTotal.Text = dtrSelect["t_total"].ToString();
            lblRPhone.Text = dtrSelect["u_phone"].ToString();
            lblREmail.Text = dtrSelect["u_email"].ToString();

            String[] lines = dtrSelect["address"].ToString().Split('|');
            String address = lines[0] + ",<br />" + lines[1] + ",<br />" + lines[2] + " " + lines[3] + ",<br />" + lines[4] + ", " + lines[5] + ".";
            lblAddress.Text = address;

            dtrSelect.Close();

            cmdSelect = new SqlCommand("SELECT username FROM Transactions t, Users u WHERE u.u_id=t.t_seller AND t.t_id=@tid", conn);
            cmdSelect.Parameters.AddWithValue("@tid", Request.QueryString["id"].ToString());
            lblSeller.Text = cmdSelect.ExecuteScalar().ToString();

            conn.Close();
        }

        public double processData(object quantity, object price)
        {
            double total = int.Parse(quantity.ToString()) * double.Parse(price.ToString());
            return total;
        }
    }
}